package com.goldman.oss.common.internal.processor;


import com.goldman.oss.common.internal.pojo.CommonFileWrapper;
import com.goldman.oss.common.internal.pojo.MultipartFileWrapper;
import com.goldman.oss.common.internal.pojo.UploadFileInfo;
import com.goldman.oss.common.internal.processor.context.ProcessorContext;
import com.goldman.oss.common.platform.FileStorage;
import com.goldman.oss.common.utils.FileUtils;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.tika.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Accessors(chain = true)
@Data
public class UploadProcessor {

    private String path;
    private Long size;
    private String filename;
    private String originalFilename;
    private MultipartFile multipartFile;
    private File file;
    private String businessCode;
    private boolean publicRead;

    public UploadFileInfo upload() {
        MultipartFileWrapper fileWrapper = new MultipartFileWrapper();
        BeanUtils.copyProperties(this, fileWrapper);
        if (StringUtils.isBlank(filename)) {
            filename = FileUtils.generateFileName(multipartFile);
            fileWrapper.setFilename(filename);
        }
        fileWrapper.setExt(FileUtils.getFileExt(multipartFile));
        FileStorage fileStorage = ProcessorContext.getFileStorage(businessCode);
        return fileStorage.store(fileWrapper);
    }

    public UploadFileInfo uploadFile() {
        CommonFileWrapper commonFileWrapper = new CommonFileWrapper();
        BeanUtils.copyProperties(this, commonFileWrapper);
        if (StringUtils.isBlank(filename)) {
            filename = FileUtils.generateFileName(file);
            commonFileWrapper.setFilename(filename);
        }
        commonFileWrapper.setExt(FileUtils.getFileExt(file));
        FileStorage fileStorage = ProcessorContext.getFileStorage(businessCode);
        return fileStorage.store(commonFileWrapper);
    }

}
